﻿using AutoMapper;
using MediatR;
using S.Archives.Api.Application.Command;
using S.Archives.Infrstructre.InterFace;

namespace S.Archives.Api.Application.CommandHandlers
{
    public class TransplantStateEditCommandHandler : IRequestHandler<TransplantStateEditCommand, bool>
    {
        private readonly ISheepFileRepository sheepFileRepository;
        private readonly IMapper mapper;
        public TransplantStateEditCommandHandler(ISheepFileRepository sheepFileRepository, IMapper mapper)
        {
            this.sheepFileRepository = sheepFileRepository;
            this.mapper = mapper;
        }
        /// <summary>
        /// 后裔转种
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<bool> Handle(TransplantStateEditCommand request, CancellationToken cancellationToken)
        {
              var sheel= sheepFileRepository.GetAll().Where(x=>request.EarTag.Contains(x.EarTag));
            foreach (var item in sheel)
            {
                if (item.Sex == 1)
                {
                    item.FileType = 1;
                }
                if (item.Sex == 2)
                {
                    item.FileType = 2;
                }
                item.UpdateDate=DateTime.Now;
            }
            return sheepFileRepository.UpdateRangeAsync(sheel);
        }
    }
}
